﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace lcmyjobs.Web.reward
{
    public partial class enlist : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtjob.Attributes.Add("readonly", "true");
                txttrade.Attributes.Add("readonly", "true");
                txtarea.Attributes.Add("readonly", "true");
                


                if (Application["webid"] != null)
                {
                    int atid = Convert.ToInt32(Application["webid"]);

                    BindAscx(atid);
                    this.Title = string.Format("悬赏招聘--猎头 猎头公司 聊城猎头 聊城猎头公司 {0}", Application["webname"].ToString());
                }
                if (Request["enterprise_rid"] != null && Request["rewardId"] != null)
                {
                    int rid = Convert.ToInt32(Request["enterprise_rid"]);
                    int rewardId = Convert.ToInt32(Request["rewardId"]);
                    //this.lblid.Text = rid.ToString();
                    BindLeftInfo(rid, rewardId);
                }
                else
                {
                    if (Application["webid"] != null)
                    {
                        int atid = Convert.ToInt32(Application["webid"]);


                        int c_isfrist = 0;
                        int c_reward = 0;
                        int c_vip = 0;
                        int c_hunter = 0;
                        int c_newjob = 0;
                        int c_workerclass = 0;


                        DataSet ds = new lcmyjobs.BLL.enterprise_releasejob().GetJobsInfo(false, true, false, false, false, false,
                                                            atid, 1, 1,
                                                            out c_isfrist, out c_reward, out c_hunter, out c_vip, out c_newjob, out c_workerclass);
                        if (ds.Tables[0].Rows.Count == 1)
                        {

                            this.Response.Redirect("enlist-" + ds.Tables[0].Rows[0]["enterprise_rid"] + "-" + ds.Tables[0].Rows[0]["enterprise_reward_id"] + ".html");
                        }
                    }
                }

            }

        }

        private void BindAscx(int atid)
        {
            int c_isfrist = 0;
            int c_reward = 0;
            int c_vip = 0;
            int c_hunter = 0;
            int c_newjob = 0;
            int c_workerclass = 0;


            DataSet ds = new lcmyjobs.BLL.enterprise_releasejob().GetJobsInfo(false, true, false, false, false, false,
                                                atid, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize,
                                                out c_isfrist, out c_reward, out c_hunter, out c_vip, out c_newjob, out c_workerclass);

            this.AspNetPager1.RecordCount = c_reward;

            if (this.AspNetPager1.PageSize > this.AspNetPager1.RecordCount)
            {
                this.AspNetPager1.Visible = true;
                this.lbl_fenye.Visible = true;
            }
            else
            {
                this.AspNetPager1.Visible = false;
                this.lbl_fenye.Visible = false;
            }
            StringBuilder str = new StringBuilder("<ul>");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                setRewardjobs(str, row);
            }
            str.Append("</ul>");
            this.ltr_joblist.Text = str.ToString();
        }
        private void setRewardjobs(StringBuilder str, DataRow row)
        {
            str.Append("<li class='elite'>");
            str.Append("<a target='_blank' href='../reward/enlistjobs-");
            str.Append(row["enterprise_rid"]);
            str.Append("-");
            str.Append(row["enterprise_reward_id"]);
            str.Append(".html' title='");
            str.Append(row["jobs_name"]);
            str.Append("'>");
            str.Append("<h3>");
            str.Append(row["jobs_name"]);
            str.Append("</h3>");
            str.Append("<ul class='clear_fix'>");
            str.Append("<li class='no'><span>编号：</span>L");
            str.Append(row["enterprise_reward_id"].ToString().PadLeft(6, '0'));
            str.Append("</li>");
            str.Append("<li class='company'><span>");
            str.Append(GetStrSub(row["jobclass"].ToString(), 8));
            str.Append("</span></li>");
            str.Append("<li class='city'><span>地点：</span>");
            str.Append(GetStrSub(row["area_name"].ToString(), 7));
            str.Append("</li>");
            str.Append("<li class='timestamp'><span>发布日期：</span>");
            str.Append(row["releasetime"].ToString().Substring(0, 10));
            str.Append("</li>");
            str.Append("<li class='salary'>悬赏金额：<strong class='orange'>");
            str.Append(row["price"]);
            str.Append("元</strong></li></ul>");
            str.Append("</a></li>");

        }
        private string GetStrSub(string str, int length)
        {
            if (str.Length > length)
            {
                //return str.Substring(0, length) + "...";
                return str.Substring(0, length);
            }
            else
            {
                return str;
            }
        }
        private void BindLeftInfo(int enterprise_rid, int rewardId)
        {
            /*
            DataSet ds = new lcmyjobs.BLL.enterprise_releasejob().GetBasicAndJobsInfoByRid(enterprise_rid);

            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                this.lbl_areaname.Text = row["area_name"].ToString();
                this.lbl_bh.Text = rewardId.ToString().PadLeft(6, '0');
                this.lbl_jobduty.Text = row["jobduty"].ToString();
                this.lbl_jobsname.Text = row["jobs_name"].ToString();
                this.Title = string.Format("悬赏招聘：{1}   人才网   招聘网   求职网   猎头网   {0}", Application["webname"].ToString(), this.lbl_jobsname.Text);
                this.lbl_requirement.Text = row["jobrequirement"].ToString();

            }
            if (ds.Tables[1].Rows.Count > 0)
            {
                DataRow row = ds.Tables[1].Rows[0];
                this.lbl_dwgm.Text = row["dwgm"].ToString();
                this.lbl_trade.Text = row["trade_name"].ToString();
            }
            if (ds.Tables[2].Rows.Count > 0)
            {
                DataRow row = ds.Tables[2].Rows[0];

                this.lbl_price.Text = row["price"].ToString();
                this.lbl_rtime.Text = row["rtime"].ToString();
                //this.lbl_rewardId.Text = row["enterprise_reward_id"].ToString();
            }
        }

        protected void btn_jb_Click(object sender, EventArgs e)
        {
            string str = "offer_en.aspx?rewardBh=" + this.lbl_bh.Text + "&rewardName=" + this.lbl_jobsname.Text;
            Response.Redirect(str);
             * */
        }

        protected void btnxidan_Click(object sender, EventArgs e)
        {
            Response.Redirect("checkEnLogin.html");
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            if (Application["webid"] != null)
            {

                BindAscx(Convert.ToInt32(Application["webid"]));
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (Application["webid"] != null)
            {
                bindsearch(Convert.ToInt32(Application["webid"]));
            }
        }

        private void bindsearch(int atid)
        {
            int count = 0;
            lcmyjobs.Model.searchjobs sj = new Model.searchjobs();
            sj = GetModel();

            DataSet ds = new lcmyjobs.BLL.enterprise_releasejob().GetSearchReward(sj, atid, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out count);

            this.AspNetPager1.RecordCount = count;

            int cnt = ds.Tables[0].Rows.Count;


            if (this.AspNetPager1.RecordCount > this.AspNetPager1.PageSize)
            {
                this.AspNetPager1.Visible = true;
                lbl_fenye.Visible = true;
            }
            else
            {
                this.AspNetPager1.Visible = false;
                lbl_fenye.Visible = false;
            }

            StringBuilder str = new StringBuilder("<ul>");
            for (int i = 0; i < cnt; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                setRewardjobs(str, row);
            }
            str.Append("</ul>");
            this.ltr_joblist.Text = str.ToString();
        }

        private lcmyjobs.Model.searchjobs GetModel()
        {
            lcmyjobs.Model.searchjobs sj = new Model.searchjobs();
            int min = 0, max = 0;
            string area = txtarea.Text.Trim();
            string jobs = txtjob.Text.Trim();
            string tradename = txttrade.Text.Trim();
            string jobsClass = this.hid_jobclass.Value;
            string jobs_id = "";
            string key_words = this.txt_keywords.Text;
            if (txtYmin.Text.Trim() != "")
                min = int.Parse(txtYmin.Text.Trim());
            if (txtYmax.Text.Trim() != "")
                max = int.Parse(txtYmax.Text.Trim());
            string dwgm = ddldwgm.SelectedItem.Text;

            #region 职位
            if (!string.IsNullOrEmpty(jobs))
            {
                string[] jobsl = jobs.Split(',');
                string[] jobsClassl = jobsClass.Split(',');



                if (jobsl.Length > 0)
                {
                    for (int i = 0; i < jobsl.Length; i++)
                    {
                        DataSet ds = new lcmyjobs.BLL.jobs().GetList(" job_name = N'" + jobsl[i] + "' AND jobclassname =N'" + jobsClassl[i] + "' ");

                        jobs_id += ds.Tables[0].Rows[0]["jobs_id"].ToString() + ",";
                        //sj.job_name1 = ds.Tables[0].Rows[0]["jobs_id"].ToString();
                    }

                    jobs_id.Substring(0, jobs_id.Length - 1);

                    string[] jobs_temp = jobs_id.Split(',');
                    if (jobs_temp.Length == 1)
                    {
                        sj.job_name1 = jobs_temp[0];
                    }
                    if (jobs_temp.Length == 2)
                    {
                        sj.job_name1 = jobs_temp[0];
                        sj.job_name2 = jobs_temp[1];
                    }
                    if (jobs_temp.Length == 3)
                    {
                        sj.job_name1 = jobs_temp[0];
                        sj.job_name2 = jobs_temp[1];
                        sj.job_name3 = jobs_temp[2];
                    }
                }
            }
            #endregion

            #region 地区
            if (!string.IsNullOrEmpty(area))
            {
                string[] areas = area.Split(',');
                if (areas.Length == 1)
                {
                    sj.area_name1 = areas[0];

                    string[] t_area = areas[0].Split('•');
                    if (t_area.Length > 0)
                    {
                        sj.area_name4 = t_area[0];
                    }
                }
                if (areas.Length == 2)
                {
                    sj.area_name1 = areas[0];
                    sj.area_name2 = areas[1];

                    string[] t_area = areas[0].Split('•');
                    if (t_area.Length > 0)
                    {
                        sj.area_name4 = t_area[0];
                    }

                    string[] t_area1 = areas[1].Split('•');
                    if (t_area.Length > 0)
                    {
                        sj.area_name5 = t_area1[0];
                    }
                }
                if (areas.Length == 3)
                {
                    sj.area_name1 = areas[0];
                    sj.area_name2 = areas[1];
                    sj.area_name3 = areas[2];


                    string[] t_area = areas[0].Split('•');
                    if (t_area.Length > 0)
                    {
                        sj.area_name4 = t_area[0];
                    }

                    string[] t_area1 = areas[1].Split('•');
                    if (t_area.Length > 0)
                    {
                        sj.area_name5 = t_area1[0];
                    }

                    string[] t_area2 = areas[2].Split('•');
                    if (t_area.Length > 0)
                    {
                        sj.area_name6 = t_area2[0];
                    }
                }
            }
            #endregion

            if (!string.IsNullOrEmpty(tradename))
            {
                string[] trades = tradename.Split(',');


                if (trades.Length == 1)
                {
                    sj.tradename1 = trades[0];
                }
                if (trades.Length == 2)
                {
                    sj.tradename1 = trades[0];
                    sj.tradename2 = trades[1];
                }
                if (trades.Length == 3)
                {
                    sj.tradename1 = trades[0];
                    sj.tradename2 = trades[1];
                    sj.tradename3 = trades[2];
                }

            }

            sj.key_words = key_words;
            sj.dwgm = dwgm;
            sj.salaryymin = min;
            sj.salaryymax = max;           
            
            if (Application["webid"] != null)
            {
                sj.atid = Convert.ToInt32(Application["webid"]); ;
            }

            return sj;
        }

    }
}